package v1

import (
	"github.com/gin-gonic/gin"
	"go-admin/global"
	mr "go-admin/internal/model/api/model_request"
	ms "go-admin/internal/model/api/model_response"
	"go-admin/internal/service"
	"go-admin/pkg/app"
	"go-admin/pkg/errcode"
)

type User struct {
}

func NewUser() User {
	return User{}
}

// 创建
func (u *User) Create(c *gin.Context) {
	param := mr.CreateUserRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	insertId, err := svc.CreateUser(&param)
	if err != nil {
		global.Logger.WithCaller(2).Errorf("svc.CreateUser err: %v", err)
		response.Fail(app.WithMsg(errcode.ErrCreate))
		return
	}
	response.Ok(app.WithData(app.ToId(insertId)))
	return
}

// 获取列表
func (u *User) List(c *gin.Context) {
	param := mr.ListUserRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())

	// 获取列表总数
	totalRows, err := svc.CountUser(&param)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.CountUser errs: %v", errors)
		response.Fail(app.WithMsg(errcode.ErrGet))
		return
	}
	// 获取分页信息
	pager := app.Pager{PageIndex: app.GetPageIndex(c), PageSize: app.GetPageSize(c), TotalRows: totalRows}
	list, err := svc.GetUserList(&param, &pager)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.GetUserList errs: %v", errors)
		response.Fail(app.WithMsg(errcode.ErrGet))
		return
	}
	response.Ok(app.WithData(app.ToPageList(pager, ms.ModelToListUserRes(list))))
	return
}

func (u *User) UserDetailById(c *gin.Context) {
	param := mr.CommonId{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	user, err := svc.GetUserById(param.Id)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("app.GetUserById errs: %v", err)
		response.Fail(app.WithMsg(errcode.ErrGet))
		return
	}
	response.Ok(app.WithData(app.ToDetail(user)))
}

func (u *User) Update(c *gin.Context) {
	param := mr.UpdateUserRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	if err := svc.UpdateUser(&param); err != nil {
		global.Logger.WithCaller(2).Errorf("svc.UpdateUser err: %v", err)
		response.Fail(app.WithMsg(errcode.ErrEdit))
		return
	}
	response.Ok()
	return
}

// 批量删除
func (u *User) Deletes(c *gin.Context) {
	param := mr.CommonIds{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	if err := svc.DeleteUserS(param.Ids); err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.DeleteUserS errs: %v", errors)
		response.Fail(app.WithMsg(errcode.ErrDelete))
		return
	}
	response.Ok()
	return
}

// 用户密码登录
func (u *User) LoginByPassword(c *gin.Context) {
	param := mr.LoginByPasswordRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	tokens, err := svc.LoginByPassword(&param)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.LoginByPassword errs: %v", errors)
		response.Fail(app.WithMsg(errcode.ErrLogin))
		return
	}
	response.Ok(app.WithData(tokens))
	return
}

// 根据刷新token重新获取token
func (u *User) GetTGokenByRefreshToken(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	myClaims, exists := c.Get("myClaims")
	if !exists {
		response.Fail()
		return
	}
	claims, _ := myClaims.(*app.MyClaims)
	// 根据用户名称获取用户token
	tokens, err := svc.GetTokenByRefreshToken(claims.Username)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.GetTokenByRefreshToken errs: %v", err)
		response.Fail(app.WithMsg(errcode.ErrLogin))
		return
	}
	response.Ok(app.WithData(tokens))
	return
}

// 获取用户个人信息
func (u *User) GetUserInfo(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	myClaims, exists := c.Get("myClaims")
	if !exists {
		response.Fail()
		return
	}
	claims, _ := myClaims.(*app.MyClaims)
	userInfo, err := svc.GetUserInfo(claims.Username)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.GetUserInfo errs: %v", err)
		response.Fail(app.WithMsg(errcode.ErrLogin))
		return
	}
	response.Ok(app.WithData(userInfo))
	return
}
